package com.inter.law.action;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.inter.law.dao.IlTaskDAO;
import com.inter.law.entity.IlTask;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TaskAction extends ActionSupport{
    private ApplicationContext ctx = new ClassPathXmlApplicationContext(
			"applicationContext.xml");
	 Map<String, Object> session = ActionContext.getContext().getSession();
	 //设置Date转换对象，将Date转换成固定格式
	 DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    private Integer id;
	    private Integer caseId;
		private Integer lawerId;
		private Boolean isManager;
		private String content;
		private Date taskDateline;
		private Integer status;
		private Date startDate;
		private Date endDate;
		private Boolean isWholeDay;
		private Date startTime;
		private Date endTime;
		private String address;
		private Integer type;
		private String taskProfile;
		private Integer important;
		/**
		 * 新建Task
		 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("taskStart");
		Integer myId=(Integer) session.get("id");		
		IlTask ilTask=getTask();
		IlTaskDAO ilTaskDAO=IlTaskDAO.getFromApplicationContext(ctx);
		ilTask.setLawerId(myId);
		ilTask.setStatus(0);
		if(important==null){
			ilTask.setImportant(1);
		}else{
			ilTask.setImportant(important);
		}
		
		ilTaskDAO.save(ilTask);
		System.out.println(endTime.toString());
		
		return SUCCESS;
	}
	/**
	 * 显示Task编辑页面
	 */
	public String show() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	/**
	 * 更新Task
	 */
	public String update() throws Exception {
		// TODO Auto-generated method stub	
		IlTaskDAO ilTaskDAO=IlTaskDAO.getFromApplicationContext(ctx);
		IlTask ilTask=ilTaskDAO.findById(id);
		ilTask.setAddress(address);
		ilTask.setContent(content);
		ilTask.setEndTime(new Timestamp(endTime.getTime()));
		ilTask.setStartTime(new Timestamp(startTime.getTime()));
		ilTask.setStatus(status);
		ilTask.setIsWholeDay(isWholeDay);
		ilTask.setImportant(important);
		ilTaskDAO.attachDirty(ilTask);
		
		return SUCCESS;
	}
	/**
	 * 删除Task
	 */
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		System.out.print("id"+id+"\n");
		IlTaskDAO ilTaskDAO=IlTaskDAO.getFromApplicationContext(ctx);
		ilTaskDAO.delete(ilTaskDAO.findById(id));
		System.out.print("ok"+"\n");
		return SUCCESS;
	}
	
	/**
	 * 通过相关参数得到一个Task
	 * @return
	 */
	public IlTask getTask(){
		IlTask ilTask=new IlTask();
		ilTask.setId(id);
		ilTask.setAddress(address);
		ilTask.setCaseId(caseId);
		ilTask.setContent(content);
//		ilTask.setEndDate(new Timestamp(endDate.getTime()));
		ilTask.setEndTime(new Timestamp(endTime.getTime()));
		ilTask.setIsManager(isManager);
		ilTask.setIsWholeDay(isWholeDay);
		ilTask.setLawerId(lawerId);
//		ilTask.setStartDate(new Timestamp(startDate.getTime()));
		ilTask.setStartTime(new Timestamp(startTime.getTime()));
		ilTask.setStatus(status);
//		ilTask.setTaskDateline(new Timestamp(taskDateline.getTime()));
		ilTask.setTaskProfile(taskProfile);
		ilTask.setType(type);
		return ilTask;
	}
	public Integer getCaseId() {
		return caseId;
	}
	public void setCaseId(Integer caseId) {
		this.caseId = caseId;
	}
	public Integer getLawerId() {
		return lawerId;
	}
	public void setLawerId(Integer lawerId) {
		this.lawerId = lawerId;
	}
	public Boolean getIsManager() {
		return isManager;
	}
	public void setIsManager(Boolean isManager) {
		this.isManager = isManager;
	}
	public String getContent() {
		
		return content;
	}
	public void setContent(String content) {
		System.out.println("content"+content+"\n");
		this.content = content;
	}
	public Date getTaskDateline() {
		return taskDateline;
	}
	public void setTaskDateline(Date taskDateline) {
		this.taskDateline = taskDateline;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		System.out.println("status"+status+"\n");
		this.status = status;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		System.out.println("endDate"+endDate+"\n");
		this.endDate = endDate;
	}
	public Boolean getIsWholeDay() {
		System.out.println("isWholeDay"+isWholeDay+"\n");
		return isWholeDay;
	}
	public void setIsWholeDay(Boolean isWholeDay) {
		this.isWholeDay = isWholeDay;
	}
	public String getStartTime() {
		return sdf.format(startTime);
	}
	public void setStartTime(Date startTime) {
		System.out.println("startTime"+startTime+"\n");
		this.startTime = startTime;
	}
	public String getEndTime() {
		return sdf.format(endTime);
	}
	public void setEndTime(Date endTime) {
		System.out.println("endTime"+endTime+"\n");
		this.endTime = endTime;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		System.out.println("address"+address+"\n");
		this.address = address;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getTaskProfile() {
		return taskProfile;
	}
	public void setTaskProfile(String taskProfile) {
		this.taskProfile = taskProfile;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		System.out.println("id"+id+"\n");
		this.id = id;
	}
	public Integer getImportant() {
		return important;
	}
	public void setImportant(Integer important) {
		this.important = important;
	}
	
	
	
}
